﻿<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="Security.Cryptography"%>
<%@ Import Namespace="AD.BLL" %>
<%@ Import Namespace="AD.Model" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script type="text/C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string clientkey = Request.QueryString["clientkey"];
            if (!string.IsNullOrEmpty(clientkey))
            {
                try
                {
                    byte[] bytes = new AESHelper().Decrypt(UtilHepler.HexToByte(clientkey));
                    if (bytes != null)
                    {
                        string userId = Encoding.UTF8.GetString(bytes);
                        UserMod user = UserBLL.QueryById(int.Parse(userId));
                        PayTradeMod pay = new PayTradeMod()
                            {
                                Out_Trade_No = "jld_" + DateTime.Now.ToString("yyyyMMddHHmmss"),
                                RewardType = PayTradeType.Invite,
                                UserId = int.Parse(userId),
                                RequestIp = Request.UserHostAddress,
                                RewardPoints = HttpDataCache.AppSettings.RewardBalancePoint,
                                Commit_Date = DateTime.Now,
                                InviteId = this.Request.RawUrl,
                                Balance = user.BalancePoint + HttpDataCache.AppSettings.RewardBalancePoint,
                                Status = HttpDataCache.AppSettings.IsValidReward,
                                IsInMoney = true,
                            };
                        if (PayTradeBLL.CheckIP(pay) == null)
                        {
                            if (PayTradeBLL.Insert(pay) > 0)
                            {
                                if (PayTradeBLL.UpdateInMoneyStatus(pay.Out_Trade_No) > 0)
                                {
                                    pay = PayTradeBLL.QueryByTradeNo(pay.Out_Trade_No);
                                    if (pay != null)
                                        if (HttpDataCache.AppSettings.IsValidReward)
                                            UtilHepler.UpdateAccountFlow(user, "system", pay.RewardPoints, OperationType.Invite);
                                }
                            }
                        }
                    }
                }
                catch { }
            }
            string fileName = ConfigurationManager.AppSettings["InstallFile"];
            ToDownload(Server.MapPath("~/") + fileName);
        }
        finally
        {
            string location = UtilHepler.GetLocation(Request.UserHostAddress);
            DownloadLogBLL.Insert(new DownloadLogMod() { UserName = "system", CommitDate = DateTime.Now, IpAddress = Request.UserHostAddress, LocationMsg = location });
        }
    }

    protected void ToOpen(string filePath)
    {
        FileInfo info = new FileInfo(filePath);
        string fileName = info.Name;
        long fileSize = info.Length;
        Response.AddHeader("Content-Length", fileSize.ToString());
        Response.Clear();
        Response.ClearHeaders();
        Response.AppendHeader("Content-Disposition", "inline;filename=" + HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(fileName)));
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(filePath);
        Response.Flush();
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }

    protected void ToDownload(string filePath)
    {
        FileInfo info = new FileInfo(filePath);
        string fileName = info.Name;
        long fileSize = info.Length;
        Response.AddHeader("Content-Length", fileSize.ToString());
        Response.Clear();
        Response.ClearHeaders();
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(fileName)));
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(filePath);
        Response.Flush();
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
</body>
</html>
